<?php
/**
 * Pertyler
 * Object Browser's Table's Objects Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require '../library/Pert/General/Link.php';

require '../library/Pert/General/Page.php';

require '../databaseModel/Table.php';

require '../databaseModel/Role.php';

require '../control/main.php';

if(empty(\Pert\Page::Get()->HREF()->ParentTable))
	return false;

if(empty(\Pert\Page::Get()->HREF()->ParentRow))
	return false;

if(empty(\Pert\Page::Get()->HREF()->Table))
	return false;

if(empty(\Pert\Page::Get()->HREF()->Column))
	return false;

if(!($moderator = Role_Moderator::Select()))
	if(require '../control/Login.php')
		return;

$parentTable = Table::Select(\Pert\Page::Get()->HREF()->ParentTable);

$parentRow = $parentTable->SelectRow(\Pert\Page::Get()->HREF()->ParentRow);

$table = Table::Select(\Pert\Page::Get()->HREF()->Table);

$columnArray = $table->SelectColumnArray();

$column = $columnArray[\Pert\Page::Get()->HREF()->Column];

require 'KoolControls/KoolAjax/koolajax.php';

require 'KoolControls/KoolTreeView/kooltreeview.php';

$treeView = new \KoolTreeView('treeView');

$treeView->imageFolder = $iconsFolder;

$treeView->showLines = true;

if($table->ConnectorTable())
	foreach($table->SelectRelatedPublishedRowArray($parentRow, $column) AS $row)
	{
		$link = new \Pert\Link('ObjectBrowser.php',
				'body');

		$link->ParentTable = $table->SelectParentTable()->Id();

		$link->Row = $row->SelectParent()->Id();

		$treeView->Add('root',
				$row->SelectParent()->Id(),
				$link->HTML($row->SelectParent()),
				false,
				'page.gif');
	}
else
	foreach($table->SelectRelatedPublishedRowArray($parentRow, $column) AS $row)
	{
		$link = new \Pert\Link('ObjectBrowser.php', 'body');

		$link->ParentTable = $table->Id();

		$link->Row = $row->Id();

		$treeView->Add('root',
				$row->Id(),
				$link->HTML($row),
				false,
				'page.gif');
	}

$treeView->isSubTree = true;

$treeView->width = '250px';

echo $treeView->Render();
